package com.ac.scum.log.web.model.sms;

import com.ac.scum.log.web.dto.SendPhoneCodeSmsDTO;
import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class SMSService {
    private static final String KEY = "LTAI5tG556cnJQXZJ6cj58xN";
    private static final String SECRET = "zriUKcJfrPUpOJrWzkINivn8FihVFS";

    private void sendAliSMS(SendPhoneCodeSmsDTO sendPhoneCodeSmsDTO) {


        try {
            StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder().accessKeyId(KEY).accessKeySecret(SECRET).build());
            SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                    .phoneNumbers(sendPhoneCodeSmsDTO.getPhone())
                    .signName(sendPhoneCodeSmsDTO.getSignName())
                    .outId(sendPhoneCodeSmsDTO.getOutId())
                    .templateCode(sendPhoneCodeSmsDTO.getTemplateCode())
                    .templateParam(new ObjectMapper().writeValueAsString(Map.of("code", sendPhoneCodeSmsDTO.getCode())))
                    .build();
            AsyncClient client = AsyncClient.builder().credentialsProvider(provider).overrideConfiguration(ClientOverrideConfiguration.create().setEndpointOverride("dysmsapi.aliyuncs.com")).build();
            CompletableFuture<SendSmsResponse> sendSmsResponseCompletableFuture = client.sendSms(sendSmsRequest);
            client.close();
            log.info(sendSmsResponseCompletableFuture.toString());
        } catch (Exception e) {
            log.error(" send log error : {}", e.getMessage());
        }
    }

    public HttpResponse<String> sendUniSMS(SendPhoneCodeSmsDTO sendPhoneCodeSmsDTO) {
        try {
            Gson gson = new Gson();

            HttpResponse<String> response = HttpClient.newHttpClient().send(HttpRequest.newBuilder()
                    .uri(URI.create("https://uni.apistd.com/?action=sms.message.send&accessKeyId=P7CDrLWP7zaPqpYFReSvZ6hRDMWtsQxquy4tn1qthwG8wDYsi"))
                    .header("Content-Type", "application/json") // 设置请求头，这里假设发送JSON数据
                    .POST(HttpRequest.BodyPublishers.ofString(gson.toJson(Map.of(
                            "to", sendPhoneCodeSmsDTO.getPhone(),
                            "signature", sendPhoneCodeSmsDTO.getSignName(),
                            "templateId", sendPhoneCodeSmsDTO.getTemplateCode(),
                            "templateData", sendPhoneCodeSmsDTO.getData()
                    )))) // 设置body内容
                    .build(), HttpResponse.BodyHandlers.ofString());
            log.info("通过UniSMS向{}发送一条短信内容{} ： 响应\n{}", sendPhoneCodeSmsDTO.getPhone(), sendPhoneCodeSmsDTO.getData().toString(), response.body());
            return response;
        } catch (Exception e) {
            log.error(" send log error : {}", e.getMessage());
        }
        return null;
    }

}
